package com.css.zfzx.sjcj.modules.classifycode.repository.impl;

import com.css.zfzx.sjcj.modules.classifycode.repository.ZfzxClassifyCodeNativeRepository;
import com.css.zfzx.sjcj.modules.classifycode.repository.entity.ZfzxClassifyCodeEntity;
import com.css.zfzx.sjcj.modules.classifycode.service.impl.SingletonClassifyCode;
import com.css.zfzx.sjcj.modules.classifycode.viewobjects.ZfzxClassifyCodeQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * @author tao
 * @date 2020-11-06
 */
@Repository
public class ZfzxClassifyCodeNativeRepositoryImpl implements ZfzxClassifyCodeNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(ZfzxClassifyCodeNativeRepositoryImpl.class);

//    public ZfzxClassifyCodeNativeRepositoryImpl(){
//        super();
//
//        SingletonClassifyCode.getInstance().setCodeRepository(this);
//    }

    @Override
    public Page<ZfzxClassifyCodeEntity> queryZfzxClassifyCodes(ZfzxClassifyCodeQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from zfzx_classify_code ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getId())) {
            whereSql.append(" and id = :id");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCode())) {
            whereSql.append(" and code = :code");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getLevel())) {
            whereSql.append(" and level = :level");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getParentId())) {
            whereSql.append(" and parent_id = :parentId");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getGroupId())) {
            whereSql.append(" and group_id = :groupId");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCodeName())) {
            whereSql.append(" and code_name = :codeName");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getIsValid())) {
            whereSql.append(" and is_valid = :isValid");
        } else {
            whereSql.append(" and is_valid = 1");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCreateTime())) {
            whereSql.append(" and create_time = :createTime");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCreateUser())) {
            whereSql.append(" and create_user = :createUser");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getUpdateTime())) {
            whereSql.append(" and update_time = :updateTime");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getUpdateUser())) {
            whereSql.append(" and update_user = :updateUser");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getRemark())) {
            whereSql.append(" and remark = :remark");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), ZfzxClassifyCodeEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from zfzx_classify_code ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if(!PlatformObjectUtils.isEmpty(queryParams.getId())) {
            query.setParameter("id", queryParams.getId());
            countQuery.setParameter("id", queryParams.getId());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCode())) {
            query.setParameter("code", queryParams.getCode());
            countQuery.setParameter("code", queryParams.getCode());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getLevel())) {
            query.setParameter("level", queryParams.getLevel());
            countQuery.setParameter("level", queryParams.getLevel());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getParentId())) {
            query.setParameter("parentId", queryParams.getParentId());
            countQuery.setParameter("parentId", queryParams.getParentId());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getGroupId())) {
            query.setParameter("groupId", queryParams.getGroupId());
            countQuery.setParameter("groupId", queryParams.getGroupId());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCodeName())) {
            query.setParameter("codeName", queryParams.getCodeName());
            countQuery.setParameter("codeName", queryParams.getCodeName());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getIsValid())) {
            query.setParameter("isValid", queryParams.getIsValid());
            countQuery.setParameter("isValid", queryParams.getIsValid());
        } else {
            query.setParameter("isValid", "1");
            countQuery.setParameter("isValid", "1");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCreateTime())) {
            query.setParameter("createTime", queryParams.getCreateTime());
            countQuery.setParameter("createTime", queryParams.getCreateTime());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCreateUser())) {
            query.setParameter("createUser", queryParams.getCreateUser());
            countQuery.setParameter("createUser", queryParams.getCreateUser());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getUpdateTime())) {
            query.setParameter("updateTime", queryParams.getUpdateTime());
            countQuery.setParameter("updateTime", queryParams.getUpdateTime());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getUpdateUser())) {
            query.setParameter("updateUser", queryParams.getUpdateUser());
            countQuery.setParameter("updateUser", queryParams.getUpdateUser());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getRemark())) {
            query.setParameter("remark", queryParams.getRemark());
            countQuery.setParameter("remark", queryParams.getRemark());
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<ZfzxClassifyCodeEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<ZfzxClassifyCodeEntity> zfzxClassifyCodePage = new PageImpl<>(list, pageable, count.longValue());
        return zfzxClassifyCodePage;
    }
}
